<template>
  <a-modal
    :open="open"
    width="900px"
    :title="rowData?.id ? '修改国家医保病种' : '新增国家医保病种'"
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    centered
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '130px' } }">
      <a-form-item label="病种目录代码" class="w-2/6" v-bind="validateInfos.diseTypeCode">
        <a-input v-model:value="formState.diseTypeCode" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="病种名称" class="w-4/6" v-bind="validateInfos.diseTypeName">
        <a-input v-model:value="formState.diseTypeName" placeholder="请输入" @change="onChangeDiseTypeName" />
      </a-form-item>
      <a-form-item label="治疗方式代码" class="w-2/6">
        <a-input v-model:value="formState.treatmentCode" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="治疗方式名称" class="w-4/6">
        <a-input v-model:value="formState.treatmentName" placeholder="请输入" />
      </a-form-item>
      <!-- v-bind="validateInfos.oprName" -->
      <!-- <a-form-item label="手术操作名称" class="w-full">
        <a-input-group compact class="w-full">
          <SelectTable
            v-model:value="formState.oprName"
            type="手术icd9"
            class="!w-2/3"
            @select=";(formState.oprCode = $event.icd9Code), (formState.oprName = $event.icd9Name)"
          />
          <a-form-item-rest>
            <a-input v-model:value="formState.oprCode" class="!w-1/3" :disabled="true" placeholder="请选择" />
          </a-form-item-rest>
        </a-input-group>
      </a-form-item> -->
      <a-form-item label="手术操作名称" class="w-full">
        <a-input-group compact class="w-full">
          <a-input v-model:value="formState.oprName" class="!w-2/3" placeholder="请输入手术操作名称" />
          <a-form-item-rest>
            <a-input v-model:value="formState.oprCode" class="!w-1/3" placeholder="请输入手术操作代码" />
          </a-form-item-rest>
        </a-input-group>
      </a-form-item>
      <a-form-item label="治疗目录代码" class="w-2/6">
        <a-input v-model:value="formState.dirCode" placeholder="请输入" />
      </a-form-item>
      <div class="w-4/6 flex">
        <a-form-item label="医保区划" class="w-1/2">
          <a-input v-model:value="formState.nmiAreaCode" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="助记码" class="w-1/2">
          <a-input v-model:value="formState.zjm" placeholder="请输入" />
        </a-form-item>
      </div>
      <!-- <a-form-item label="手术操作代码" class="w-2/6">
        <a-input v-model:value="formState.oprCode" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="手术操作名称" class="w-4/6">
        <a-input v-model:value="formState.oprName" placeholder="请输入" />
      </a-form-item> -->
      <a-form-item label="唯一记录号" class="w-2/6">
        <a-input v-model:value="formState.recordSn" placeholder="请输入" />
      </a-form-item>
      <div class="w-4/6 flex">
        <a-form-item label="版本号" class="w-1/2">
          <a-input v-model:value="formState.version" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="版本名称" class="w-1/2">
          <a-input v-model:value="formState.versionName" placeholder="请输入" />
        </a-form-item>
      </div>
      <a-form-item label="有效状态" class="w-2/6">
        <a-switch v-model:checked="formState.status" />
      </a-form-item>
      <div class="w-4/6 flex">
        <a-form-item label="诊疗指南页码" class="w-1/2">
          <a-input v-model:value="formState.manualPage" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="诊疗指南电子档案" class="w-1/2">
          <a-input v-model:value="formState.manualEfile" placeholder="请输入" />
        </a-form-item>
      </div>

      <a-form-item label="病种内涵" class="w-full">
        <a-textarea v-model:value="formState.diseTypeMeaning" :rows="3" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.memo" :rows="3" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { debounce } from 'lodash-es'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'ok'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  diseTypeCode: '',
  diseTypeName: ''
  // oprName: ''
})
const rulesState = ref({
  diseTypeCode: [{ required: true, message: '请输入病种目录代码' }],
  diseTypeName: [{ required: true, message: '请输入病种名称' }]
  // oprName: [{ required: true, message: '请选择手术操作名称' }]
})
const { isCompleted, resetFields, validateInfos } = useForm(formState, rulesState)
const onChangeDiseTypeName = debounce(async () => {
  const { data, code } = await post('/common/util/getPinyin', { name: formState.value.diseTypeName })
  code === 0 && (formState.value.zjm = data)
}, 500)
const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
const onOk = async () => {
  formState.value.status = formState.value.status ? 0 : 1
  const { code, msg } = await post('/insure/doSaveNmiDictDiseType', formState.value)
  if (code !== 0) return message.error(msg)
  message.success(msg)
  emit('update:open', false)
  emit('ok')
}

watch(
  () => props.open,
  () => {
    if (props.rowData?.id) {
      formState.value = { ...props.rowData }
    } else {
      formState.value = { status: true }
    }
  }
)

watch(
  () => formState.value.oprName,
  (val) => {
    if (!val) {
      formState.value.oprCode = ''
    }
  }
)
</script>
<style lang="less" scoped></style>
